import { _decorator, Component, Node, Label, Button } from 'cc';
import { BaseView } from '../../../../../scripts/libs/rxjs/cc3/BaseView';
import { BlockView } from './BlockView';
import { ReactiveProperty } from '../../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { BindButton } from '../../../../../scripts/libs/rxjs/cc3/BindButton';
import { adsm } from '../../../../../scripts/managers/AdsManager';
import { gm } from '../../../../../scripts/managers/GameManager';
import { concat, forkJoin, Subscription } from 'rxjs';
import { nullifySubscription } from '../../../../../scripts/libs/rxjs/utils';
import { IntervalStartNow, Timer } from '../../../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { AnimationPlay } from '../../../../../scripts/libs/rxjs/cc3/AnimationPlay';
import { Animation } from 'cc';
import { StageView } from './StageView';
import { TweenToStart } from '../../../../../scripts/libs/rxjs/cc3/TweenStart';
import { v3 } from 'cc';
import { instantFunc } from '../../../../../scripts/libs/rxjs/cc3/CommonObservable';
import { UIOpacity } from 'cc';
import { misc_sdk_m } from '../../../../../scripts/managers/SDK/MiscSDKManager';
import { thinking_sdk_m } from '../../../../../scripts/managers/SDK/ThinkingCocosSDKManager';
import { udm } from '../../../../../scripts/managers/UserDataManager';
const { ccclass, property } = _decorator;

@ccclass('WaitingPlaceView')
export class WaitingPlaceView extends BaseView {

    stageView: StageView = null;

    idx: number = null;

    orderInSix: number = null; // 在6个中的顺序位置

    @property(Node)
    container: Node = null;

    blockView: BlockView = null;

    animating = ReactiveProperty.Create(false); // 是否正在播放向前的动画

    @property(Node)
    layoutNode: Node = null;

    @property(Label)
    stageNumLabel: Label = null;

    @property(Button)
    plateAdsButton: Button = null;

    @property(Node)
    placeRed: Node = null;

    @property(Node)
    plateNode: Node = null;

    lockStageNum = ReactiveProperty.Create(null);

    useObserves() {
        return [
            this.lockStageNum.subscribe(this.refresh.bind(this)),
            BindButton(this.plateAdsButton).subscribe(this.plateAdsButtonClicked.bind(this)),
        ];
    }

    onDestroyView(): void {
        nullifySubscription.call(this, '_sub_red_shine');
        nullifySubscription.call(this, '_sub_red_anim');
        nullifySubscription.call(this, '_sub_zoom_hide');
    }

    refresh() {
        if (this.lockStageNum.value != null) {
            this.layoutNode.active = true;
            this.stageNumLabel.string = `Level ${this.lockStageNum.value}`;
            this.plateAdsButton.interactable = true;
            gm.currentAdsType.value = 'AdExtraPlate';
            gm.currentAdsOpenUI.value = 'GamePanel';
            thinking_sdk_m.requestTrack('Ads_Show', {
                AdsType: gm.currentAdsType.value,
                OpenUI: gm.currentAdsOpenUI.value,
                IsNew: udm.isNew.value
            });
        } else {
            this.layoutNode.active = false;
            this.plateAdsButton.interactable = false;
        }
    }

    plateAdsButtonClicked() {
        if (gm.isWin.value || gm.isLose.value) return;
        gm.currentAdsType.value = 'AdExtraPlate';
        gm.currentAdsOpenUI.value = 'GamePanel';
        thinking_sdk_m.requestTrack('Ads_Click', {
            AdsType: gm.currentAdsType.value,
            OpenUI: gm.currentAdsOpenUI.value,
            IsNew: udm.isNew.value
        });
        adsm.showRewardAds('add_plate').subscribe(_ => {
            if (_ == 'success') {
                thinking_sdk_m.requestTrack('Ads_Return', {
                    AdsType: gm.currentAdsType.value,
                    OpenUI: gm.currentAdsOpenUI.value,
                    IsNew: udm.isNew.value
                });
                gm.openWaitingPlaceCommand.execute(this.orderInSix);
            }
        }).addTo(this.node);
    }

    _sub_red_shine: Subscription = null;
    _sub_red_anim: Subscription = null;
    startShine() {
        nullifySubscription.call(this, '_sub_red_shine');
        let curCount = 0;
        this._sub_red_shine = IntervalStartNow(1, this.node).subscribe(_ => {
            nullifySubscription.call(this, '_sub_red_anim');
            this._sub_red_anim = AnimationPlay(this.placeRed.getComponent(Animation), 'place_red').subscribe();
            curCount++;
            if (curCount >= 2) {
                nullifySubscription.call(this, '_sub_red_shine');
            }
        })
    }

    endShine() {
        nullifySubscription.call(this, '_sub_red_shine');
    }

    _sub_zoom_hide: Subscription = null;
    zoomHideAndShow() {
        nullifySubscription.call(this, '_sub_zoom_hide');
        let _v3 = v3(this.node.worldPosition);
        _v3.y += 20;
        this.plateNode.getComponent(UIOpacity).opacity = 255;
        this._sub_zoom_hide = concat(
            Timer(.2, this.node),
            TweenToStart(this.plateNode, .2, { scale: v3(0, 0, 0) }, { easing: 'backIn' }),
            instantFunc(() => {
                this.stageView.genBlinkPlateDestroy(_v3);
                misc_sdk_m.requestHapic('heavy');
            }),
            Timer(.5, this.node),
            instantFunc(() => {
                this.plateNode.getComponent(UIOpacity).opacity = 0;
                this.plateNode.setScale(.93, .93, .93);
                this.plateNode.setPosition(0, -20, 0);
            }),
            forkJoin([
                TweenToStart(this.plateNode.getComponent(UIOpacity), .5, { opacity: 255 }, { easing: 'quadOut' }),
                TweenToStart(this.plateNode, .5, { position: v3(0, 0, 0) }, { easing: 'quadOut' }),
            ])
        ).subscribe();
    }
}

